var _ = require('lodash');
var async = require('async');
var moment = require('moment');
var recordModel = require('./recordModel.js');
var roleModel = require('../../app/models/roleModel.js');
var configUtil = require('../../app/util/configUtil.js');

suite('refresh_total_pay_day', function () {
    suite('refresh_totla_pay_day', function () {
        test('刷新玩家累计充值天数', function (done) {
            var server_id = configUtil.load('game').server_id;
            recordModel.getPayList(server_id, function (err, pay_model_list) {
                async.eachSeries(pay_model_list, function(pay_model_item, cb) {
                    var role_id = pay_model_item.role_id;
                    roleModel.getByRoleId(role_id , function(err, role_model) {
                        var now_pay_day = moment(pay_model_item.create_time).startOf('day').unix();
                        console.log('role_id:', role_id);
                        console.log('last_pay_day:', role_model.last_pay_day);
                        console.log('now_pay_day:', now_pay_day);
                        if (now_pay_day > role_model.last_pay_day) {
                            role_model.total_pay_day++;
                            role_model.last_pay_day = now_pay_day;
                            role_model.save(cb);
                        } else {
                            cb();
                        }
                    });
                }, function (err) {
                    if (!!err) {
                        console.error(err);
                        return done(err);
                    }
                    console.log('操作完成');
                    done();
                });
            });
        });
    });
});
